﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using HtmlAgilityPack;

namespace ExpiredDomainSearch.Manager
{
    public class SearchManager
    {
        private string DownloadSearchContent(string keyword, string ext, string filter, string hypen, string number, string ncharacters, string searchdays, string page)
        {
            string value = "";
            try
            {
                WebClient client = new WebClient();
                string formUrl = string.Format("http://cachelogic.net/embed/script/index.php?page={7}&domain={0}&ext={1}&filter={2}&hyphen={3}&numbers={4}&ncharacter={5}&searchdays={6}&action=search",
                                                keyword, ext, filter, hypen, number, ncharacters, searchdays, page);

                value = client.DownloadString(formUrl);
            }
            catch
            {
            }

            return value;
        }

        public SearchResult GetSearchedDomains(string keyword, string ext, string filter, string hypen, string number, string ncharacters, string searchdays, string page)
        {
            string content = DownloadSearchContent(keyword, ext, filter, hypen, number, ncharacters, searchdays, page);
            SearchResult result = new SearchResult();
            List<DomainItem> domains = new List<DomainItem>();
            if (!string.IsNullOrWhiteSpace(content))
            {
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(content);
                HtmlNodeCollection tableNodes = doc.DocumentNode.SelectNodes("//table[@id='newspaper-a']");
                if (tableNodes != null && tableNodes.Count>0)
                {
                    doc.LoadHtml(tableNodes[0].OuterHtml);
                    HtmlNodeCollection threadNodes = doc.DocumentNode.SelectNodes("//thead");
                    if (threadNodes != null && threadNodes.Count > 0)
                    {
                        doc.LoadHtml(threadNodes[0].OuterHtml);
                        HtmlNodeCollection countNodes = doc.DocumentNode.SelectNodes("//td");
                        if (countNodes != null && countNodes.Count > 0)
                        {
                            result.TotalCount = countNodes[0].InnerText;
                        }
                    }

                    doc.LoadHtml(content);
                    HtmlNodeCollection tbodyNodes = doc.DocumentNode.SelectNodes("//tbody");
                    if (tbodyNodes != null && tbodyNodes.Count > 0)
                    {
                        doc.LoadHtml(tbodyNodes[0].OuterHtml.Replace("<tr>", "</tr><tr>"));
                        HtmlNodeCollection domainNodes = doc.DocumentNode.SelectNodes("//tr");
                        if (domainNodes != null && domainNodes.Count > 0)
                        {
                            HtmlDocument domainRowDoc = new HtmlDocument();
                            foreach (HtmlNode domainNode in domainNodes)
                            {
                                domainRowDoc.LoadHtml(domainNode.OuterHtml);
                                HtmlNodeCollection detailNodes = domainRowDoc.DocumentNode.SelectNodes("//td");
                                if (detailNodes != null && detailNodes.Count == 3)
                                {
                                    domains.Add(new DomainItem()
                                    {
                                        DomainName = detailNodes[0].InnerText,
                                        PendingDelete = detailNodes[1].InnerText,
                                        DeletionDate = detailNodes[2].InnerText
                                    });
                                }
                            }
                        }
                    }
                }
            }

            result.DomainList = domains;

            return result;
        }
    }

    public class SearchResult
    {
        public string TotalCount { get; set; }
        public List<DomainItem> DomainList { get; set; }
    }

    public class DomainItem
    {
        public string DomainName { get; set; }
        public string PendingDelete { get; set; }
        public string DeletionDate { get; set; }
    }
}